package com.grandtech.insurance.common.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class ossFileUtil {

    public static String uploadAliyun(MultipartFile file, String fileName) throws IOException {
        // 1 获取上传需要的固定值
        String endpoint ="oss-cn-shanghai.aliyuncs.com";      //你的站点
        String accessKeyId = "LTAIP816Jan0Nvjq";  //你的acess_key_id
        String accessKeySecret = "lm9mo4aBFhzjvP8CejjEP3d7HPEi9n"; //你的acess_key_secret
        String bucketName = "ram-insurance";       //你的bucket_name
        String key = OssConstant.filedir + generateRouterKey(fileName);
        //外面获取文件输入流，最后方便关闭
        InputStream in = file.getInputStream();
        AliOssClientParamDO aliOssClientParamDO = new AliOssClientParamDO();
        try {
            //2 创建OssClient对象
            OSSClient ossClient = getClient(aliOssClientParamDO);
            //3 获取文件信息，为了上传
            // meta设置请求头
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType("image/jpg");
            //4 设置知道文件夹

            ossClient.putObject(bucketName,fileName,in, meta);
            //5 关闭ossClient
            ossClient.shutdown();
            //6 返回上传之后地址，拼接地址
            String uploadUrl = "https://"+bucketName+"."+endpoint+"/"+fileName;
            return uploadUrl;
        }catch(Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            in.close();
        }
    }
    /**
     * 获取OSSClient
     *
     * @param aliOssClientParamDO
     */
    private static OSSClient getClient(AliOssClientParamDO aliOssClientParamDO) {
        if (aliOssClientParamDO == null) {
            aliOssClientParamDO = new AliOssClientParamDO();
        }
        if (StringUtils.isBlank(aliOssClientParamDO.getEndpoint())) {
            aliOssClientParamDO.setEndpoint(OssConstant.endpoint);
        }
        if (StringUtils.isBlank(aliOssClientParamDO.getBucketName())) {
            aliOssClientParamDO.setBucketName(OssConstant.bucketName);
        }
        if (StringUtils.isBlank(aliOssClientParamDO.getAccessKeyId())) {
            aliOssClientParamDO.setAccessKeyId(OssConstant.accessKeyId);
        }
        if (StringUtils.isBlank(aliOssClientParamDO.getAccessKeySecret())) {
            aliOssClientParamDO.setAccessKeySecret(OssConstant.accessKeySecret);
        }
        OSSClient ossClient = new OSSClient(aliOssClientParamDO.getEndpoint(), aliOssClientParamDO.getAccessKeyId(),
                aliOssClientParamDO.getAccessKeySecret());
        return ossClient;
    }

    /**
     * 生成key的规则
     *
     * @param filename
     * @return
     */
    private static String generateRouterKey(String filename) {
        // 年月日文件夹命名
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString().split("-");
        // 路径加入uuid
        String uuid = UUID.randomUUID().toString();
        // 去掉“-”符号
        uuid = uuid.replaceAll("-", "");
        // 拼接路径
        String path = strNow[0] + "/" + strNow[1] + "/" + strNow[2] + "/" + uuid + "/";
        return path + filename;
    }
}
